﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenefitHelper;
namespace benefit.Controllers
{
    public class GroupController : Controller
    {



        public ViewResult Create()
        {
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            List<BenefitHelper.Sys.Groups> groups = new BenefitHelper.Sys.Groups().GetGroups(db);
            ViewBag.groups = groups;
            ViewBag.operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
            db.Dispose();

            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                ViewBag.iid = "1";
            }
            else
            {
                ViewBag.iid = "";
            }

            return View();
        }

        [HttpPost]
        public ActionResult Create(BenefitHelper.Sys.Groups oper)
        {
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            int groupid = oper.AddGroup(db);
            string masters = Request.Form["master"];
            if (!string.IsNullOrEmpty(masters))
            {
                string[] masterarray = masters.Split(new char[] { ',' });
                foreach (string s in masterarray)
                {
                    BenefitHelper.Sys.GroupMaster gm = new BenefitHelper.Sys.GroupMaster();
                    gm.MasterId = Convert.ToInt32(s);
                    gm.GroupId = groupid;
                    gm.Insert(db, gm);
                }
            }
            db.SaveChanges();

            db.Dispose();

            if (!string.IsNullOrEmpty(Request.Form["iid"]))
            {
                return RedirectToAction("AddNewGroups", "Init", new { type = "1" });
            }
            else
            {
                return RedirectToAction("AddNewGroups", "Init");
            }


        }

        public ActionResult Edit(int id)
        {
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var query = db.Groups.Where(a => a.Id == id).First();
            List<BenefitHelper.Sys.Groups> groups = new BenefitHelper.Sys.Groups().GetGroups(db);
            ViewBag.groups = groups;
            ViewBag.operators = new BenefitHelper.Sys.Operator().GetUnDeletedOperators(db);
            ViewBag.masters = new BenefitHelper.Sys.GroupMaster().GetGroupMasterId(id, db);
            db.Dispose();
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                ViewBag.iid = "1";
            }
            else
            {
                ViewBag.iid = "";
            }
            return View(query);
        }

        [HttpPost]
        public ActionResult Edit(BenefitHelper.Sys.Groups oper)
        {
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            int groupid = oper.Id;
            var groupquery = db.Groups.Find(groupid);
            groupquery.GroupName = oper.GroupName;
            groupquery.ParentGroupId = oper.ParentGroupId;



            string masters = Request.Form["master"];
            var query = db.GroupMaster.Where(a => a.GroupId == oper.Id);
            foreach (BenefitHelper.Sys.GroupMaster gm in query)
            {
                db.GroupMaster.Remove(gm);
            }
            db.SaveChanges();
            if (!string.IsNullOrEmpty(masters))
            {
                string[] masterarray = masters.Split(new char[] { ',' });
                foreach (string s in masterarray)
                {
                    BenefitHelper.Sys.GroupMaster gm = new BenefitHelper.Sys.GroupMaster();
                    gm.MasterId = Convert.ToInt32(s);
                    gm.GroupId = groupid;
                    gm.Insert(db, gm);
                }
            }
            db.SaveChanges();

            db.Dispose();

            if (!string.IsNullOrEmpty(Request.Form["iid"]))
            {
                return RedirectToAction("AddNewGroups", "Init", new { type = "1" });
            }
            else
            {
                return RedirectToAction("AddNewGroups", "Init");
            }


        }

        public ActionResult Delete(int id)
        {
            BenefitHelper.DB.DBManager db = new BenefitHelper.DB.DBManager();
            var query = db.Groups.Find(id);
            query.IsDeleted = true;
            db.SaveChanges();

            var query2 = db.GroupMaster.Where(a => a.GroupId == id).ToList();
            foreach (BenefitHelper.Sys.GroupMaster master in query2)
            {
                db.GroupMaster.Remove(master);
            }
            db.SaveChanges();
            db.Dispose();
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
            {
                return RedirectToAction("AddNewGroups", "Init", new { type = "1" });
            }
            else
            {
                return RedirectToAction("AddNewGroups", "Init");
            }
        }
    }
}